package com.fccommunity.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.fccommunity.R;
import com.fccommunity.adapter.BannerPagerAdapter;
import com.fccommunity.adapter.NewsAdapter;
import com.fccommunity.adapter.base.RecycleViewItemListener;
import com.fccommunity.base.BannerData;
import com.fccommunity.base.BaseFragment;
import com.fccommunity.ui.GZCXActivity;
import com.fccommunity.ui.GZJFActivity;
import com.fccommunity.ui.MessageActivity;
import com.fccommunity.ui.WYBXActivity;
import com.fccommunity.ui.WebViewActivity;
import com.fccommunity.ui.ZCZXActivity;
import com.fccommunity.widget.AutoScrollViewPager;
import com.fccommunity.widget.DivItemDecoration;
import com.fccommunity.widget.TitleBar;
import com.malinskiy.superrecyclerview.SuperRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yiwei on 16/4/17.
 */
public class GZFWFragment extends BaseFragment{

    private AutoScrollViewPager viewpage;
    private BannerPagerAdapter mBannerAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_gzfw, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewpage = (AutoScrollViewPager) view.findViewById(R.id.bannerVp);

        List<BannerData> bannerDatas = new ArrayList<>();
        BannerData data1 = new BannerData();
        data1.id = "0";
        data1.imgUrl = "http://banbao.chazidian.com/uploadfile/2016-01-25/s145368924044608.jpg";
        BannerData data2 = new BannerData();
        data2.id = "1";
        data2.imgUrl = "http://pic32.nipic.com/20130829/12906030_124355855000_2.png";

        bannerDatas.add(data1);
        bannerDatas.add(data2);
        mBannerAdapter = new BannerPagerAdapter(getContext(), bannerDatas);
        viewpage.setAdapter(mBannerAdapter);
        viewpage.startAutoScroll();

        //
        LinearLayout gzjfLl = (LinearLayout) view.findViewById(R.id.gzjfLl);
        LinearLayout wybxLl = (LinearLayout) view.findViewById(R.id.wybxLl);
        LinearLayout gzcxLl = (LinearLayout) view.findViewById(R.id.gzcxLl);
        LinearLayout zczxLl = (LinearLayout) view.findViewById(R.id.zczxLl);

        gzjfLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(GZJFActivity.newIntent(getContext()));
            }
        });
        wybxLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(WYBXActivity.newIntent(getContext()));
            }
        });
        gzcxLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(GZCXActivity.newIntent(getContext()));
            }
        });
        zczxLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(ZCZXActivity.newIntent(getContext()));
            }
        });

        initListView(view);

    }

    private void initListView(View view) {
        SuperRecyclerView recyclerView = (SuperRecyclerView) view.findViewById(R.id.recycleView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // Do your refresh
            }
        });
        NewsAdapter adapter = new NewsAdapter();
        adapter.setItemListener(new RecycleViewItemListener() {
            @Override
            public void onItemClick(int position) {
                String url = "http://www.baidu.com";
                startActivity(WebViewActivity.newIntent(getActivity(),"", url));
            }

            @Override
            public boolean onItemLongClick(int position) {
                return false;
            }
        });
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new DivItemDecoration(2));
    }

    @Override
    protected void initTitle() {
        titleBar.setTitle("蜂巢社区");
        titleBar.addAction(new TitleBar.ImageAction(R.mipmap.icon_ring) {
            @Override
            public void performAction(View view) {
                startActivity(new Intent(getContext(), MessageActivity.class));
            }
        });
    }

}
